
package handlers;

import communication.KatanaClient;
import game.Player;
import shared.KatanaConstants;
import shared.KatanaPacket;
import shared.Opcode;

public abstract class PacketHandler
{
    public static void handlePacket(KatanaClient client, KatanaPacket packet)
    {
        if(packet == null)
            return;
        
        switch(packet.getOpcode())
        {
            case C_LOGOUT:      handleLogoutPacket(client, packet);     break;
            case C_REGISTER:    handleRegisterPacket(client, packet);   break;
            case C_LOGIN:       handleLoginPacket(client, packet);      break;
            case C_PONG:        handlePongPacket(client, packet);       break;
            default:
                System.err.println("PacketHandler: Received unknown opcode: " + packet.getOpcode());
                break;
        }
    }
    
    private static Player loginClient(String username, String password, KatanaClient client)
    {
        if(username == null || username.isEmpty() || password == null || password.isEmpty() || client == null)
            return null;
        
        /*
        ArrayList<HashMap<String, Object>> results = SQLHandler.instance().runLoginQuery(username, password);
        if(results == null || results.size() != 1) // Bad login
            return null;
        
        int id = (Integer)results.get(0).get("user_id");
        // Check if player is already logged in
        if(GameHandler.instance().getPlayer(id) != null)
        {
            System.err.println("Player " + id + " attempted to log in but is already authenticated!");
            return null;
        }
        
        Player player = new Player(id, 
                                   username,
                                   0,
                                   client);
        
        client.setPlayer(player);
        KatanaServer.instance().removeWaitingClient(client);
        GameHandler.instance().addPlayer(id, player);
        */
        System.out.println("PacketHandler: loginClient NYI");
        
        Player player = null;
        return player;
    }
    
    // No data
    private static void handleLogoutPacket(KatanaClient client, KatanaPacket packet)
    {
        client.remove(true);
    }
    
    // Packet data format expected to be username / password
    private static void handleRegisterPacket(KatanaClient client, KatanaPacket packet)
    {
        String[] data = packet.getData().split(KatanaConstants.PACKET_DATA_SEPERATOR);
        if(data.length < 2)
        {
            System.err.println("handleRegisterPacket: Invalid data in packet (" + packet.getData() + ")");
            // Response
            KatanaPacket response = new KatanaPacket(Opcode.S_REG_NO);
            client.sendPacket(response);
            return;
        }
        
        String username = data[0].trim();
        String password = data[1];
        
        /*
        SQLHandler sql = SQLHandler.instance();
        ArrayList results = sql.runUsernameQuery(username);
        if(results != null && !results.isEmpty())
        {
            // Handle case where the user reinstalled the app (if password matches database,log her in)
            Player player = loginClient(username, password, client);
            if(player == null)
            {
                System.err.println("handleRegisterPacket: Username [" + username + "] already exists in database. Prevent registration");

                KatanaPacket response = new KatanaPacket(Opcode.S_REG_NO);
                client.sendPacket(response);
                return;
            }
            else
            {
                KatanaPacket response = new KatanaPacket(Opcode.S_AUTH_OK);
                response.addData(player.getId() + "");;
                player.sendPacket(response);
                
                return;
            }
        }
        
        // New user, create her account then log her in
        sql.runRegisterUserQuery(username, password);
        Player player = loginClient(username, password, client);
        if(player == null)
        {
            KatanaPacket response = new KatanaPacket(Opcode.S_REG_NO); // Somehow something went wrong....
            client.sendPacket(response);
            return;
        }
        
        // Yay! Success!
        KatanaPacket response = new KatanaPacket(Opcode.S_REG_OK);
        response.addData(player.getId() + "");
        player.sendPacket(response);
        */
        
        System.out.println("PacketHandler: Register NYI");
    }
    
    // Data format: username, password
    private static void handleLoginPacket(KatanaClient client, KatanaPacket packet)
    {
        String[] data = packet.getData().split(KatanaConstants.PACKET_DATA_SEPERATOR);
        if(data.length < 2)
        {
            System.err.println("handleLoginPacket: Invalid data in packet (" + packet.getData() + ")");
            // Response
            KatanaPacket response = new KatanaPacket(Opcode.S_AUTH_NO);
            client.sendPacket(response);
            return;
        }
        
        String username = data[0].trim();
        String password = data[1];
        
        /*
        System.out.println("handleLoginPacket: User [" + username + "] logged in");
        Player player = loginClient(username, password, client);
        if(player == null)
        {
            KatanaPacket response = new KatanaPacket(Opcode.S_AUTH_NO);
            client.sendPacket(response);
            return;
        }
        
        // Login success!
        KatanaPacket response = new KatanaPacket(Opcode.S_AUTH_OK);
        response.addData(player.getId() + "");
        player.sendPacket(response);
        */
        
        System.out.println("PacketHandler: login NYI");
    }
    
    // No data - unnecessary
    private static void handlePongPacket(KatanaClient client, KatanaPacket packet)
    {
        // what should we do in here
        client.pongReceived();
    }
}
